# -*- coding: utf-8 -*-
# Create your views here.
from wikicamp.operlist.models import *
from wikicamp.operlist.forms import *
from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponseRedirect,HttpResponse
from django.template import RequestContext
from django.conf import Settings,settings
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm,UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User,Permission,Group
from django.contrib.auth.decorators import *



import datetime
import locale


def index(request):
    form = AuthenticationForm()
    hlist = Hasta.objects.filter()
    return render_to_response('operlist/index.html', {'authform': form,'hlist':hlist},context_instance=RequestContext(request))


def operlist_logout(request):
    logout(request)
    return HttpResponseRedirect("/operlist/")

def operlist_login(request):
    form = AuthenticationForm()
    return render_to_response('operlist/accounts.html', {'authform': form},context_instance=RequestContext(request))

def operlist_add(request):
    form = HastaForm()
    return render_to_response('operlist/index.html', {'addform': form},context_instance=RequestContext(request))

def operlist_saveas(request):
    
    if request.method=="POST":
        
        form = HastaForm(request.POST)
        
        if not form.is_valid():
        
            return render_to_response('operlist/index.html',{'addform':form,},context_instance=RequestContext(request))
        
        else:
        
            cd=form.cleaned_data
            hasta = Hasta(name=cd['name'],aciklama=cd['aciklama'],durum=cd['durum'])
            hasta.save()
         
        hlist = Hasta.objects.filter()
        return render_to_response('operlist/index.html',{'addform':form,'hlist':hlist},context_instance=RequestContext(request))

def operlist_delete(request):
    
    xid = request.GET.get('id')
    
    try:
        Hasta.objects.filter(id=xid).delete()
    except Hasta.DoesNotExist:
        #ilerde hata sinifini dolduracak sekilde duzenlenecek
        hata = u"hata olustu, hasta kaydi silinemedi..."
        print hata
    
    return HttpResponseRedirect("/operlist/")

        
    
    
def operlist_show(request):
    hlist= Hasta.objects.filter()
    return render_to_response('operlist/show.html',{'hlist':hlist},context_instance=RequestContext(request))

def operlist_show_h(request):
    hlist= Hasta.objects.filter()
    return render_to_response('operlist/show-h.html',{'hlist':hlist},context_instance=RequestContext(request))

def operlist_ajax(request):
   
    return render_to_response('operlist/ajax.html',{},context_instance=RequestContext(request))

def operlist_ajax_h(request):
   
    return render_to_response('operlist/ajax-h.html',{},context_instance=RequestContext(request))
